package dou.servletShopp;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class ProductDAO {
	public List<Product> findAll() throws Exception {
		List<Product> products = new ArrayList<Product>();
		Connection conn = null;
		try {
			conn = DBUtil.getConnection();
			Statement stat = conn.createStatement();
			ResultSet rs = stat.executeQuery("select * from chang_product");
			while (rs.next()) {
				Product p = new Product();
				p.setId(rs.getInt("id"));
				p.setModel(rs.getString("model"));
				p.setPic(rs.getString("pic"));
				p.setProdDesc(rs.getString("prodDesc"));
				p.setPrice(rs.getDouble("price"));
				products.add(p);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn);
		}
		return products;
	}

	public Product findById(int id) {
		Product p = null;
		Connection conn = null;
		try {
			conn = DBUtil.getConnection();
			Statement stat = conn.createStatement();
			ResultSet rs = stat.executeQuery("select * from chang_product where id=" + id);
			while (rs.next()) {
				p = new Product();
				p.setId(rs.getInt("id"));
				p.setModel(rs.getString("model"));
				p.setPic(rs.getString("pic"));
				p.setProdDesc(rs.getString("prodDesc"));
				p.setPrice(rs.getDouble("price"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn);
		}
		return p;
	}
}
